package com.hfzy.ihk.web.freeswitchManage.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.hfzy.ihk.facade.freeswitchManage.enums.TrunkTypeEnum;
import com.hfzy.ihk.facade.websocket.linkcall.service.WebSocketService;
import com.hfzy.ihk.web.freeswitchManage.utils.SpringWebSocketUtils;
import com.hfzy.ihk.web.freeswitchManage.websocket.swx.WebSocketManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;

/**
 * czs
 * 将消息推送给websocket 服务器类
 */
@Component
@Slf4j
public class PushToWebsocketBiz {
//
//    @Reference(version = "1.0.0",check = false,retries=0,timeout = 5000)
//    private WebSocketService webSocketService;
    /**
     * 推送消息到设备
     * @param serverId
     * @param data
     * @return
     */
    public Boolean pushMessage(String serverId,String data){
        boolean rs = true;
        try {
            if (serverId == null){

                log.info(">>推送消息去设备失败serverId为空");
                return false;
            }

            WebSocketSession webSocketSession = WebSocketManager.swxServerIdWebSocketSessionMap.get(serverId);
            if (webSocketSession == null) {
                log.info(">>推送消息去设备失败webSocketSession为空");
                return false;
            }
            log.info("query str to swx str:{}",data);
            SpringWebSocketUtils.pushToClient(webSocketSession, data);
            log.info("push to swx rs:{}",rs);
            return true;
            // rs = webSocketService.pushToSwxByServerId(serverId, data);

        }catch (Exception e){
            log.info("推送消息到swx 设备异常：serverId:{},data:{}",serverId,data,e);
            rs = false;
        }
        return rs;

    }

    /**
     * 查询设备卡槽信息
     * @param serverId
     * @return
     */
    public Boolean queryDevicesInfo(String serverId){
        HashMap<String,String> map = new HashMap<>();
        map.put("Action","SWXShowDevices");
        return pushMessage(serverId, JSON.toJSONString(map));
    }

    /**
     * 切换通道卡的激活状态
     * @return
     */
    public Boolean changeActiveCard(String  serverId ,String channel,String slotID ){
        HashMap<String,String> map = new HashMap<>();
        map.put("Action","SWXChangeSIMSlot");
        map.put("ActionID",serverId);
        map.put("Channel",channel);
        map.put("SlotID",slotID);
        return pushMessage(serverId, JSON.toJSONString(map));
    }

    /**
     * 当换新的SIM卡时，需要清除一次SIM卡的统计信息
     */
    public Boolean ClearSlotStatis(String  serverId ,String channel,String slotID ){
        HashMap<String,String> map = new HashMap<>();
        map.put("Action","SWXClearStatis");
        map.put("ActionID",serverId);
        map.put("Channel",channel);
        map.put("SlotID",slotID);
        return pushMessage(serverId, JSON.toJSONString(map));
    }


    /**
     * 查询通道中继，可以定时检查队列中的通道状态
     * @param serverId
     * @param
     * @return
     */
    public Boolean checkChannelTrunk(String  serverId){
        HashMap<String,String> map = new HashMap<>();
        map.put("Action","TrunkList"); //中继事件名
        map.put("ActionID", TrunkTypeEnum.SWX.getValue()); //命令原样返回
        return pushMessage(serverId, JSON.toJSONString(map));
    }

    /**
     * 当机器重启的时候，调用该方法查询freeswitch中继状态，ip 和sip 账号等信息
     * @param serverId
     * @return
     */
    public Boolean checFreeswitchkTrunk(String  serverId ){
        HashMap<String,String> map = new HashMap<>();
        map.put("Action","TrunkList"); //中继事件名
        map.put("ActionID",TrunkTypeEnum.SIP.getValue()); //命令原样返回
        map.put("trunkName","freeswitch");//查询指定中继，不带该参数视为查询全部


        return pushMessage(serverId, JSON.toJSONString(map));
    }

}


